package pers.xx.sbm.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pers.xx.sbm.utils.GifCaptcha;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * @author ：xiexing
 * @description：验证码Controller
 * @date ：2020/9/4 16:02
 */
@Controller
public class VerifyCodeController {

    private final static Logger logger = LoggerFactory.getLogger(VerifyCodeController.class);

    /**
     * @param response
     * @description 获取验证码（Gif版本）
     * @author XieXing
     * @date 2020/9/4 17:26
     */
    @RequestMapping(value = "/getGifCode", method = RequestMethod.GET)
    public void getGifCode(HttpServletResponse response, HttpServletRequest request) {
        //告诉客户端，输出的格式
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/gif");
        GifCaptcha gifCaptcha = new GifCaptcha(200, 80, new Font("宋体", Font.BOLD, 40), 100);
        try {
            gifCaptcha.out(response.getOutputStream());
            logger.info("获取验证码！验证码字符为：" + gifCaptcha.getWord());
        } catch (IOException e) {
            logger.info("生成验证码错误：" + e);
        }
    }
}
